package com.soft.controller;

import com.soft.pojo.User;
import com.soft.service.UserService;
import com.soft.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-27 14:49
 */
@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("name");
        String password = req.getParameter("pwd");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        UserService userService = new UserService();
        Result result;
        result = userService.findUserByKey(user);
        if (result.getSuccess()){
            result = userService.addUser(user);
        }
//        resp.getWriter().print(result.getMsg());

        req.setAttribute("addUserMsg", result.getMsg());
        req.getRequestDispatcher("showAllUserByPage").forward(req,resp);
    }
}
